package com.john.springboot.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.john.springboot.entity.User;
import com.john.springboot.mapper.UserMapper;
import com.john.springboot.service.IUserService;

/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
@CacheConfig(cacheNames = "user")
public class UserServiceImpl implements IUserService {

	private static final String KEY_ALL = "'UserS-*'";
	
    @Resource
	private UserMapper userMapper;

	@Override
	@Cacheable(key = KEY_ALL)
	public List<User> selectListBySQL() {
		return userMapper.selectListBySQL();
	}
	@Cacheable(key = "#userId")
	@Override
	public User getUserById(Long userId) {
		return userMapper.selectById(userId);
	}
    @Override
    @CachePut(key = "#user.hotelId")
    @CacheEvict(key = KEY_ALL)
    public User create(User user) {
        	user.insert();
            return user;
    }
    @Caching(evict = {@CacheEvict(key = KEY_ALL)}, put = {@CachePut(key = "#user.id")})
    public User update(User user)  {
    	userMapper.updateAllColumnById(user);
         return user;
    }
    @Override
    @Caching(evict = {@CacheEvict(key = KEY_ALL)}, put = {@CachePut(key = "#userId")})
    public boolean delete(Long userId){
        try {
            return userMapper.deleteById(userId)==1;
        } catch (Exception ex) {
            throw  ex;
        }
    }
	@Override
	public void deleteAll() {
		 userMapper.deleteAll();
	}

}